package com.newway.wechat.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.back.entity.NearbyPlaces;
import com.newway.back.mapper.NearbyPlacesMapper;
import com.newway.common.context.UserInfoContext;
import com.newway.common.result.Result;
import com.newway.wechat.dto.NearByDto;
import com.newway.wechat.dto.NearbyDataLocation;
import com.newway.wechat.dto.PlaceBookingRecord;
import com.newway.wechat.mapper.PlaceBookingRecordMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/wechat/nearby")
public class NearByPlaceController {


    @Resource
    NearbyPlacesMapper nearbyPlacesMapper;

    @Resource
    PlaceBookingRecordMapper placeBookingRecordMapper;

    /**
     * 根据当前位置查询附近数据
     * @param nearByDto
     * @return
     */
    @GetMapping("/locationData")
    public Result getNearbyData(NearByDto nearByDto){
        if(StringUtils.isBlank(nearByDto.getLatitude())||StringUtils.isBlank(nearByDto.getLongitude())){
            return Result.fail("获取用户位置出错");
        }
        List<NearbyDataLocation> list =  nearbyPlacesMapper.getNearbyDataLocation(nearByDto);
        return Result.success(list);
    }

    @GetMapping("/details/{id}")
    public Result getPlaceById(@PathVariable("id")Integer id){
        NearbyPlaces nearbyPlaces = nearbyPlacesMapper.selectById(id);
        return Result.success(nearbyPlaces);

    }

    @GetMapping("/list")
    public Result list(NearByDto nearByDto){
        Page<NearbyPlaces> nearbyPlacesPage = new Page<>(nearByDto.getPageNum(),nearByDto.getPageSize());
        QueryWrapper<NearbyPlaces> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(nearByDto.getKeyWords())){
            queryWrapper.like("name",nearByDto.getKeyWords());
            queryWrapper.or();
            queryWrapper.like("address",nearByDto.getKeyWords());
        }
        queryWrapper.orderByDesc("id");
        Page<NearbyPlaces> result = nearbyPlacesMapper.selectPage(nearbyPlacesPage, queryWrapper);
        return Result.success(result);
    }

    @PostMapping("/booking")
    public Result bookingPlace(@RequestBody List<PlaceBookingRecord> placeBookingRecords){
        for(int i=0;i<placeBookingRecords.size();i++){
            PlaceBookingRecord placeBookingRecord = placeBookingRecords.get(i);
            NearbyPlaces nearbyPlaces = nearbyPlacesMapper.selectById(placeBookingRecord.getPlaceId());
            if(nearbyPlaces==null){
                continue;
            }
            placeBookingRecord.setPlaceName(nearbyPlaces.getName());
            placeBookingRecord.setPlaceAddress(nearbyPlaces.getAddress());
            placeBookingRecord.setStatus("0");//已申请/待审核
            placeBookingRecord.setCreateTime(LocalDateTime.now());
            placeBookingRecord.setUserId(UserInfoContext.getUserInfo().getId());
            placeBookingRecordMapper.insert(placeBookingRecord);
        }

        return Result.success();
    }
}
